package user.location;

import module.location.Coordinates;
import module.location.CoordinatesHandler;
import module.location.CoordinatesUpdateListener;
import module.location.LocationIdentifier;
import android.app.Activity;
import android.content.*;
import android.location.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class UserLocationActivity extends Activity implements CoordinatesUpdateListener
{
	// Acquire a reference to the system Location Manager
	private LocationIdentifier identifier;
	private Button button;
	private TextView text;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (TextView) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        
        //Location stuff
        CoordinatesHandler.getCoordinatesHandler().addCoordinatesListener(this);
        identifier = new LocationIdentifier((LocationManager) this.getSystemService(Context.LOCATION_SERVICE));
        
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				identifier.startLocationSearching();
				button.setVisibility(0);
			}
		});
    }
    
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    	CoordinatesHandler.getCoordinatesHandler().removeCoordinatesListener(this);
    }

	@Override
	public void onNewCoordinates(Coordinates coordinates)
	{
		text.append(coordinates.getLongitude() + " ; " + coordinates.getLatitude() + " ; " + coordinates.getAccuracy() + "\n");
		if (Float.parseFloat(coordinates.getAccuracy()) < 50.0)
		{
			identifier.stopLocationSearching();
			button.setVisibility(1);
		}
	}
}